package sqlengine.queryExecution;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import sqlengine.conditions.ConditionExecption;
import sqlengine.conditions.Conditions;
import sqlengine.database.DataFile;
import sqlengine.database.DataManager;
import sqlengine.queryOptimization.PlanOptimizer;
import sqlengine.queryOptimization.QueryOptimizationException;

public class QueryExecutionTest {

	@Test
	public void testExecuteOneTable() {
		try {
			// create table A
			String fileName = "A";
			Map<String, Integer> descriptor = new HashMap<String, Integer>();
			descriptor.put("age", 50);
			descriptor.put("height", 50);
			DataFile dataFile = DataManager.createFile(fileName, descriptor);
			
			// insert data
			Map<String, String> record = new HashMap<String, String>();
			record.put("age", "16");
			record.put("height", "170");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("age", "16");
			record.put("height", "172");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("age", "18");
			record.put("height", "172");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("age", "18");
			record.put("height", "170");
			dataFile.insertRecord(record);
			
			// create index
			dataFile.createIndex("_A.a", "age");
			
			// create condition
			Conditions condition = new Conditions("A");
			condition.addConditions("A", "age", "16");
			
			// create planOptimizer
			PlanOptimizer planOptimizer = new PlanOptimizer(condition);
			
			// create queryExecution
			QueryExecution queryExecution = new QueryExecution(planOptimizer, condition);
			
			// execute query
			String result = queryExecution.execute();
			
			System.out.println(result);
			
			DataManager.exit();
		} catch (ConditionExecption e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (QueryOptimizationException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (BasicOperationException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}

	@Test
	public void testExecuteTwoTable() {
		try {
			// create table A
			String fileName = "A";
			Map<String, Integer> descriptor = new HashMap<String, Integer>();
			descriptor.put("age", 50);
			descriptor.put("height", 50);
			DataFile dataFile = DataManager.createFile(fileName, descriptor);
			
			//insert data
			Map<String, String> record = new HashMap<String, String>();
			record.put("age", "16");
			record.put("height", "170");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("age", "16");
			record.put("height", "172");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("age", "18");
			record.put("height", "172");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("age", "18");
			record.put("height", "170");
			dataFile.insertRecord(record);
			
			//create index
			dataFile.createIndex("_A.a", "age");
			
			// create table B
			fileName = "B";
			descriptor = new HashMap<String, Integer>();
			descriptor.put("sex", 50);
			descriptor.put("old", 50);
			descriptor.put("year", 50);
			dataFile = DataManager.createFile(fileName, descriptor);
			
			// insert data
			record = new HashMap<String, String>();
			record.put("sex", "male");
			record.put("old", "18");
			record.put("year", "18");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("sex", "female");
			record.put("old", "16");
			record.put("year", "16");
			dataFile.insertRecord(record);
			
			record = new HashMap<String, String>();
			record.put("sex", "male");
			record.put("old", "16");
			record.put("year", "18");
			dataFile.insertRecord(record);
			
			// create index
			dataFile.createIndex("_B.old", "old");
			
			// create condition
			Conditions condition = new Conditions("A","B");
			//condition.addConditions("A", "age", "16");
			condition.addConditions("A", "age", "B","old");
			condition.setProjectAll(true);
//			condition.addProjections("A", "age");
//			condition.addProjections("B", "old");
			
			// create planOptimizer
			PlanOptimizer planOptimizer = new PlanOptimizer(condition);
			
			// create queryExecution
			QueryExecution queryExecution = new QueryExecution(planOptimizer, condition);
			
			// execute query
			String result = queryExecution.execute();
			
			System.out.println(result);
			
			DataManager.exit();
		} catch (ConditionExecption e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (QueryOptimizationException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (BasicOperationException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}

}
